#include "mir.h"
#include <cassert>
#include <time.h>
#include "util.h"
#include "cnfield.h"

#include "nmfield.h"
#include "nmfield.cpp"

using namespace std;
using namespace opsrc;
using namespace opsrc::stdf;

Mir::Mir()
{
}

Mir::Mir(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf != 0)
        readFields(buf);
}

Mir::~Mir() {
}


const StdfField<long> Mir::getSetupTime() const {
    return setupt;
}

void Mir::setSetupTime(StdfField<long> ti) {
    setupt = ti;
}


const StdfField<long> Mir::getStartTime() const {
    return startt;
}

void Mir::setStartTime(StdfField<long> starttime) {
    startt = starttime;
}

const StdfField<int> Mir::getStartNum() const {
    return stat_num;
}

void Mir::setStartNum(StdfField<int> startnum) {
    stat_num = startnum;
}

const StdfField<string> Mir::getModeCode() const {
    return mod_cod;
}


void Mir::setModeCode(StdfField<string> mc) {
    mod_cod = mc;
}

const StdfField<string> Mir::getRetestCode() const {
    return rtst_cod;
}

void Mir::setRetestCode(StdfField<string> retestcode) {
    rtst_cod = retestcode;
}

const StdfField<string> Mir::getProductCode() const {
    return prot_cod;
}


void Mir::setProductCode(StdfField<string> pc) {
    prot_cod = pc;
}


const StdfField<long> Mir::getBurnTime() const {
    return burn_tim;
}

void Mir::setBurnTime(StdfField<long> longt) {
    burn_tim = longt;
}

const StdfField<string> Mir::getCmodCode() const {
    return cmod_cod;
}


void Mir::setCmodCode(StdfField<string> cmodc) {
    cmod_cod = cmodc;
}


const StdfField<string> Mir::getLotId() const {
    return lot_id;
}

void Mir::setLotId(StdfField<string> lotid) {
    lot_id = lotid;
}


const StdfField<string> Mir::getPartType() const {
    return part_typ;
}


void Mir::setPartType(StdfField<string> parttype) {
    part_typ = parttype;
}

const StdfField<string> Mir::getNodeName() const {
    return node_nam;
}

void Mir::setNodeName(StdfField<string> nodename) {
    node_nam = nodename;
}

const StdfField<string> Mir::getTesterType() const {
    return tstr_typ;
}

void Mir::setTesterType(StdfField<string> testertype) {
    tstr_typ = testertype;
}


const StdfField<string> Mir::getJobName() const {
    return job_nam;
}

// This is the program name
void Mir::setJobName(StdfField<string> jobNm) {
    job_nam = jobNm;
}


const StdfField<string> Mir::getJobRevision() const {
    return job_rev;
}

void Mir::setJobRevision(StdfField<string> jobrevision) {
    job_rev = jobrevision;
}


const StdfField<string> Mir::getSubLotId() const {
    return sblot_lid;
}

void Mir::setSubLotId(StdfField<string> slotlid) {
    sblot_lid = slotlid;
}

const StdfField<string> Mir::getOperatorName() const {
    return oper_nam;
}

void Mir::setOperatorName(StdfField<string> opName) {
    oper_nam = opName;
}

const StdfField<string> Mir::getExecutionType() const {
    return exec_typ;
}


void Mir::setExecutionType(StdfField<string> execType) {
    exec_typ = execType;
}

const StdfField<string> Mir::getExecutiveVersion() const {
    return exec_ver;
}


void Mir::setExecutiveVersion(StdfField<string> execVersion) {
    exec_ver = execVersion;
}


const StdfField<string> Mir::getTestCode() const {
    return test_cod;
}

void Mir::setTestCode(StdfField<string> testcode) {
    test_cod = testcode;
}

const StdfField<string> Mir::getTestTemperature() const {
    return tst_temp;
}

void Mir::setTestTemperature(StdfField<string> testTemp) {
    tst_temp = testTemp;
}


const StdfField<string> Mir::getUserText() const {
    return user_txt;
}

void Mir::setUserText(StdfField<string> userText) {
    user_txt = userText;
}


const StdfField<string> Mir::getAuxilaryFile() const {
    return aux_file;
}

void Mir::setAuxilaryFile(StdfField<string> auxfile) {
    aux_file = auxfile;
}


const StdfField<string> Mir::getPackageType() const {
    return pkg_typ;
}

void Mir::setPackageType(StdfField<string> packageType) {
    pkg_typ = packageType;
}

const StdfField<string> Mir::getFamilyId() const {
    return family_id;
}

void Mir::setFamilyId(StdfField<string> familyId) {
    family_id = familyId;
}

const StdfField<string> Mir::getDateCode() const {
    return date_cod;
}

void Mir::setDateCode(StdfField<string> dateCode) {
    date_cod = dateCode;
}

const StdfField<string> Mir::getTestFacilityId() const {
    return facil_id;
}

void Mir::setTestFacilityId(StdfField<string> facilityId) {
    facil_id = facilityId;
}

const StdfField<string> Mir::getTestFloorId() const {
    return floor_id;
}

void Mir::setTestFloorId(StdfField<string> floorId) {
    floor_id = floorId;
}

const StdfField<string> Mir::getProcessId() const {
    return proc_id;
}
void Mir::setProcessId(StdfField<string> procid) {
    proc_id = procid;
}

const StdfField<string> Mir::getOperationFrequency() const {
    return oper_frq;
}

void Mir::setOperationFrequency(StdfField<string> opFreq) {
    oper_frq = opFreq;
}

const StdfField<string> Mir::getSpecName() const {
    return spec_nam;
}

void Mir::setSpecName(StdfField<string> specNm) {
    spec_nam = specNm;
}

const StdfField<string> Mir::getSpecVersion() const {
    return spec_ver;
}

void Mir::setSpecVersion(StdfField<string> specVer) {
    spec_ver = specVer;
}

const StdfField<string> Mir::getFlowId() const {
    return flow_id;
}

void Mir::setFlowId(StdfField<string> flowid) {
    flow_id = flowid;
}

const StdfField<string> Mir::getSetupId() const {
    return setup_id;
}

void Mir::setSetupId(StdfField<string> setupid) {
    setup_id = setupid;
}


const StdfField<string> Mir::getDesignId() const {
    return design_id;
}

void Mir::setDesignId(StdfField<string> designid) {
    design_id= designid;
}

const StdfField<string> Mir::getEngrgId() const {
    return engrg_id;
}

void Mir::setEngrgId(StdfField<string> engrgid) {
    engrg_id = engrgid;
}

const StdfField<string> Mir::getRomCode() const {
    return rom_cod;
}

void Mir::setRomCode(StdfField<string> romcode) {
    rom_cod = romcode;
}

const StdfField<string> Mir::getSerialNumber() const {
    return serial_num;
}

void Mir::setSerialNumber(StdfField<string> serialnum) {
    serial_num = serialnum;
}

const StdfField<string> Mir::getSupervisorName() const {
    return supr_nam;
}

void Mir::setSupervisorName(StdfField<string> suprnm) {
    supr_nam = suprnm;
}


void Mir::readFields(char* buf) {
#if _DEBUG
    cout << "stdfrec= MIR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // read setup time, 0-3rd bytes
    NmField<long, 4> _setupt(buf+curpos, en);
    setupt.set(_setupt.getData(), curpos, _setupt.getByteCount(), _setupt.getTotalByteCount());
    curpos += setupt.getTotalByteCount();

    //read start time, 4-7th bytes
    if (curpos < reclen-1){    
        NmField<long, 4> _startt(buf+curpos, en);
        startt.set(_startt.getData(), curpos, _startt.getByteCount(), _startt.getTotalByteCount());
        curpos += startt.getTotalByteCount();
    }

    // read test station number 8th byte
    if (curpos < reclen-1){        
        NmField<unsigned int, 1> statnm(buf+curpos, en);
        stat_num.set(statnm.getData(), curpos, statnm.getByteCount(), statnm.getTotalByteCount());
        curpos += stat_num.getTotalByteCount();
    }

    // read mode code 9th byte
    if (curpos < reclen-1){
        CnField<string,1> modc( buf+curpos, en );
        mod_cod.set(modc.getData(), curpos, modc.getByteCount(), modc.getTotalByteCount());
        curpos += mod_cod.getTotalByteCount();
    }

    // read lot retest code 10th byte
    if (curpos < reclen-1){
        CnField<string,1> retestcode(buf+curpos, en);
        rtst_cod.set(retestcode.getData(), curpos, retestcode.getByteCount(), retestcode.getTotalByteCount());
        curpos += rtst_cod.getTotalByteCount();
    }

    // read data protection code 11th byte
    if (curpos < reclen-1){
        CnField<string,1> protcode(buf+curpos, en);
        prot_cod.set(protcode.getData(), curpos, protcode.getByteCount(), protcode.getTotalByteCount());
        curpos += prot_cod.getTotalByteCount();
    }

    // read burnin time in minutes , 12th n 13th byte
    if (curpos < reclen-1){
        NmField<long, 2> burnin(buf+curpos, en);
        burn_tim.set(burnin.getData(), curpos, burnin.getByteCount(), burnin.getTotalByteCount());
        curpos += burn_tim.getTotalByteCount();
    }

    // read command mode code
    if (curpos < reclen-1){
        CnField<string,1> cmodcod(buf+curpos, en);
        cmod_cod.set(cmodcod.getData(), curpos, cmodcod.getByteCount(), cmodcod.getTotalByteCount());
        curpos += cmod_cod.getTotalByteCount();
    }

    // Read lot id
    if (curpos < reclen-1){
        CnField<string> clotid(buf+curpos, en);
        lot_id.set(clotid.getData(), curpos, clotid.getByteCount(), clotid.getTotalByteCount());
        curpos += lot_id.getTotalByteCount();
    }
    
    // Read part type
    if (curpos < reclen-1){
        CnField<string> cparttyp(buf+curpos, en);
        part_typ.set(cparttyp.getData(), curpos, cparttyp.getByteCount(), cparttyp.getTotalByteCount());
        curpos += part_typ.getTotalByteCount();
    }

    // Read node name
    if (curpos < reclen-1){
        CnField<string> cnodenm(buf+curpos, en);
        node_nam.set(cnodenm.getData(), curpos, cnodenm.getByteCount(), cnodenm.getTotalByteCount());
        curpos += node_nam.getTotalByteCount();
    }

    // read tester type
    if (curpos < reclen-1){
        CnField<string> ctstrtyp(buf+curpos, en);
        tstr_typ.set(ctstrtyp.getData(), curpos, ctstrtyp.getByteCount(), ctstrtyp.getTotalByteCount());
        curpos += tstr_typ.getTotalByteCount();
    }

    // read job name
    if (curpos < reclen-1){
        CnField<string> cjobnm(buf+curpos, en);
        job_nam.set(cjobnm.getData(), curpos, cjobnm.getByteCount(), cjobnm.getTotalByteCount());
        curpos += job_nam.getTotalByteCount();
    }

    // read job revision
    if (curpos < reclen-1){
        CnField<string> cjobrev(buf+curpos, en);
        job_rev.set(cjobrev.getData(), curpos, cjobrev.getByteCount(), cjobrev.getTotalByteCount());
        curpos += job_rev.getTotalByteCount();
    }

    // Read sblot_id field
    if (curpos < reclen-1){
        CnField<string> cblotid(buf+curpos, en);
        sblot_lid.set(cblotid.getData(), curpos, cblotid.getByteCount(), cblotid.getTotalByteCount());
        curpos += sblot_lid.getTotalByteCount();
    }

    // Read operator name
    if (curpos < reclen-1){
        CnField<string> copernm(buf+curpos, en);
        oper_nam.set(copernm.getData(), curpos, copernm.getByteCount(), copernm.getTotalByteCount());
        curpos += oper_nam.getTotalByteCount();
    }

    // read exec_typ field
    if (curpos < reclen-1){
        CnField<string> exectyp(buf+curpos, en);
        exec_typ.set(exectyp.getData(), curpos, exectyp.getByteCount(), exectyp.getTotalByteCount());
        curpos += exec_typ.getTotalByteCount();
    }

    // Read exec_version field
    if (curpos < reclen-1){
        CnField<string> execver(buf+curpos, en);
        exec_ver.set(execver.getData(), curpos, execver.getByteCount(), execver.getTotalByteCount());
        curpos += exec_ver.getTotalByteCount();
    }

    // test code
    if (curpos < reclen-1){
        CnField<string> testcode(buf+curpos, en);
        test_cod.set(testcode.getData(), curpos, testcode.getByteCount(), testcode.getTotalByteCount());
        curpos += test_cod.getTotalByteCount();
    }

    // test temperature
    if (curpos < reclen-1){
        CnField<string> tsttmp(buf+curpos, en);
        tst_temp.set(tsttmp.getData(), curpos, tsttmp.getByteCount(), tsttmp.getTotalByteCount());
        curpos += tst_temp.getTotalByteCount();
    }

    // User txt
    if (curpos < reclen-1){
        CnField<string> usrtxt(buf+curpos, en);
        user_txt.set(usrtxt.getData(), curpos, usrtxt.getByteCount(), usrtxt.getTotalByteCount());
        curpos += user_txt.getTotalByteCount();
    }

    // Aux file
    if (curpos < reclen-1){
        CnField<string> auxfile(buf+curpos, en);
        aux_file.set(auxfile.getData(), curpos, auxfile.getByteCount(), auxfile.getTotalByteCount());
        curpos += aux_file.getTotalByteCount();
    }

    // Read package type
    if (curpos < reclen-1){
        CnField<string> packtyp(buf + curpos, en);
        pkg_typ.set(packtyp.getData(), curpos, packtyp.getByteCount(), packtyp.getTotalByteCount());
        curpos += pkg_typ.getTotalByteCount();
    }

    // Read family id
    if (curpos < reclen-1){
        CnField<string> familyid(buf+curpos, en);
        family_id.set(familyid.getData(), curpos, familyid.getByteCount(), familyid.getTotalByteCount());
        curpos += family_id.getTotalByteCount();
    }

    // Read date_cod
    if (curpos < reclen-1){
        CnField<string> datecod(buf+curpos, en);
        date_cod.set(datecod.getData(), curpos, datecod.getByteCount(), datecod.getTotalByteCount());
        curpos += date_cod.getTotalByteCount();
    }

    // Facility id
    if (curpos < reclen-1){
        CnField<string> facid(buf+curpos, en);
        facil_id.set(facid.getData(), curpos, facid.getByteCount(), facid.getTotalByteCount());
        curpos += facil_id.getTotalByteCount();
    }

    // Floor id
    if (curpos < reclen-1){
        CnField<string> floorid(buf+curpos, en);
        floor_id.set(floorid.getData(), curpos, floorid.getByteCount(), floorid.getTotalByteCount());
        curpos += floor_id.getTotalByteCount();
    }

    // Process id
    if (curpos < reclen-1){
        CnField<string> procid(buf+curpos, en);
        proc_id.set(procid.getData(), curpos, procid.getByteCount(), procid.getTotalByteCount());
        curpos += proc_id.getTotalByteCount();
    }

    // Oper Frequency id
    if (curpos < reclen-1){
        CnField<string> opfrq(buf+curpos, en);
        oper_frq.set(opfrq.getData(), curpos, opfrq.getByteCount(), opfrq.getTotalByteCount());
        curpos += oper_frq.getTotalByteCount();
    }

    // get spec name
    if (curpos < reclen-1){
        CnField<string> specnm(buf+curpos, en);
        spec_nam.set(specnm.getData(), curpos, specnm.getByteCount(), specnm.getTotalByteCount());
        curpos += spec_nam.getTotalByteCount();
    }

    // get spec version
    if (curpos < reclen-1){
        CnField<string> specver(buf+curpos, en);
        spec_ver.set(specver.getData(), curpos, specver.getByteCount(), specver.getTotalByteCount());
        curpos += spec_ver.getTotalByteCount();
    }

    // get flowid
    if (curpos < reclen-1){
        CnField<string> flowid(buf+curpos, en);
        flow_id.set(flowid.getData(), curpos, flowid.getByteCount(), flowid.getTotalByteCount());
        curpos += flow_id.getTotalByteCount();
    }

    // get  setup id
    if (curpos < reclen-1){
        CnField<string> setupid(buf+curpos, en);
        setup_id.set(setupid.getData(), curpos, setupid.getByteCount(), setupid.getTotalByteCount());
        curpos += setup_id.getTotalByteCount();
    }

    // get  design revision
    if (curpos < reclen-1){
        CnField<string> designid(buf+curpos, en);
        design_id.set(designid.getData(), curpos, designid.getByteCount(), designid.getTotalByteCount());
        curpos += design_id.getTotalByteCount();
    }

    // get eng id
    if (curpos < reclen-1){
        CnField<string> engrgid(buf+curpos, en);
        engrg_id.set(engrgid.getData(), curpos, engrgid.getByteCount(), engrgid.getTotalByteCount());
        curpos += engrg_id.getTotalByteCount();
    }

    // get  rom code
    if (curpos < reclen-1){
        CnField<string> romcod(buf+curpos, en);
        rom_cod.set(romcod.getData(), curpos, romcod.getByteCount(), romcod.getTotalByteCount());
        curpos += rom_cod.getTotalByteCount();
    }

    // get serial num
    if (curpos < reclen-1){
        CnField<string> serial(buf+curpos, en);
        serial_num.set(serial.getData(), curpos, serial.getByteCount(), serial.getTotalByteCount());
        curpos += serial_num.getTotalByteCount();
    }

    // get supervisor name
    if (curpos < reclen-1){
        CnField<string> suprnm(buf+curpos, en);
        supr_nam.set(suprnm.getData(), curpos, suprnm.getByteCount(), suprnm.getTotalByteCount());
    }
}
